/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Created on : Jan 12, 2010, 07:06:28 PM
 * @author diogo
 */

var sNumberSeparator = "."
var sNumberMask = "#.###.###"
function ApplyNumbersMask(s, sMask){
    var sOutput = "";
    var sNumbers = GetNumbers(s)
    var iNumberCount = 0;
    for (var x=0; x<sMask.length && iNumberCount<sNumbers.length; x++){
        var sLetter = sMask.substr(x,1)
        if (sLetter == "#"){
            sOutput += sNumbers.substr(iNumberCount++, 1);
        }else{
            sOutput += sLetter;
        }
    }
    return sOutput;
}

/**
* funcao para aplicar uma mascara de telefone
* @param s - uma string
* @return um telefone formatado
*/ 
function TelMask(s){
    return ApplyNumbersMask(s, sTelMask);
}

/**
* funcao para pegar apenas os numeros de uma String
* @param s - uma string de entrada.
* @return uma string soh com os numeros um atras do outro
*/ 
function GetNumbers(s){

    var sNumbers ="";
    for (var x=0; x<s.length; x++){
        var sLetter = s.substr(x,1)
        if (!isNaN(sLetter) && sLetter != " ") {
            sNumbers += sLetter;
        }
    }
    return sNumbers;

}

/**
* funcao para pegar apenas os numeros de uma String
* @param s - uma string de entrada.
* @return uma string soh com os numeros um atras do outro
*/
function GetNonNumbers(s){

    var sNonNumber ="";
    for (var x=0; x<s.length; x++){
        var sLetter = s.substr(x,1)
        if (!(!isNaN(sLetter) && sLetter != " ")) {
            sNonNumber += sLetter;
        }
    }
    return sNonNumber;

}

/**
* updates the value of a field by filtering only numbers
* ideally used at onkeyXXX eventens
* i.e.: \code
*       <input type="text"  onkeypress="UpdateNumbersOnly(this)" />
* 
* @param field - the field to update
*/ 
function UpdateNumbersOnly(field){
    setTimeout(function(){
        field.value = GetNumbers(field.value)
    },10)
}


function IsNumberInput(iKeyCode){
	return (iKeyCode >= 48 && iKeyCode <= 57)
	;
}
/**
* botoes como alt, ctrl, setas direcionais, shift sao tudo invisiveis
*/
function IsVisibleInput(iKeyCode){
	return !(iKeyCode <=46);
}




/**
* updates the value of a field by filtering only numbers.
* ideally used at onkeyXXX eventens.
* ex: \code
*       <input type="text" onkeypress="UpdateCpfMask(this)" />
*
* @param field - the field to update.
* @param sMask - the mask to apply
*/
function UpdateFieldMask(field, sMask){
    setTimeout(function(){
        field.value = ApplyNumbersMask(field.value, sMask);
    },10)
}

/**
 * function to update a field's number format
 * @see DecimalFormat
 * @param field - the field to update.
 */
function UpdateNumberDigit(field){
    setTimeout(function(){
        field.value = DecimalFormat(GetNumbers(field.value))
    },10)
}

/**
 * function to format a number <br/>
 * i.e.: fAmount = 1234567; the return is 1.234.567<br/>
 *  note: the separator is <b><u>sNumberSeparator</u></b>'s value<br/>
 *  note: does not work for negative values nor <i>float</i> values. only <i>integers</i><br/>
 */
function DecimalFormat(iAmount){
    var MoneyParts_tb =[];
    var sAmount = "" + iAmount;
    var x = sAmount.length;
    var iCount = 0;
    
    var sOutput = ""

    while (x > 0){
        //MoneyParts_tb[count++] = sAmount.substr(Math.min(0,x-3), 3);
        MoneyParts_tb[iCount++] = sAmount.substring(Math.max(0,x-3), x);
        x -= 3;
    }

    var iMax = iCount -1;
    for (var i = iMax; i >= 0; i--){
        sOutput += MoneyParts_tb[i] + sNumberSeparator;
    }
    sOutput = sOutput.substr(0, sOutput.length -1);
    /// debug:
    if (false){
        var sMessage = "";
        for (iCount in MoneyParts_tb) {
            sMessage += "\npart " + iCount + ": " + MoneyParts_tb[iCount];
        }
        alert(sMessage)
    }

    return sOutput;
}